/*
 * TramoTuberia.cpp
 *
 *  Created on: 31/07/2013
 *      Author: juan
 */

#include "TramoTuberia.h"

TramoTuberia::TramoTuberia(GLuint program) :Figure(program) {
	crearComponentes(program);
}

void TramoTuberia::addPoint(vector<float>* vector,float x,float y, float z){
	vector->push_back(x);
	vector->push_back(y);
	vector->push_back(z);
}

vector<float>* TramoTuberia::createPoints(void){
	//Aqui se crea la superficie que se barre(puntos de control)
	vector<float>* PuntosControl = new vector<float>();

	 int e=1;

	 addPoint(PuntosControl,    0*e,  -1*e, 0);
	 addPoint(PuntosControl,    1*e,  -1*e, 0);
	 addPoint(PuntosControl,    1*e,   0*e, 0);
	 addPoint(PuntosControl,    1*e,   1*e, 0);
	 addPoint(PuntosControl,  0.5*e,   1*e, 0);
	 addPoint(PuntosControl,  0.5*e, 1.5*e, 0);
	 addPoint(PuntosControl,    1*e,   2*e, 0);
	 addPoint(PuntosControl,    0*e,   2*e, 0);

	 addPoint(PuntosControl,    0*e,   2*e, 0);
	 addPoint(PuntosControl,    0*e,   2*e, 0);
	 addPoint(PuntosControl,   -1*e,   2*e, 0);
	 addPoint(PuntosControl, -0.5*e, 1.5*e, 0);
	 addPoint(PuntosControl, -0.5*e,   1*e, 0);
	 addPoint(PuntosControl,   -1*e,   1*e, 0);
	 addPoint(PuntosControl,   -1*e,   0*e, 0);
	 addPoint(PuntosControl,   -1*e,  -1*e, 0);
	 addPoint(PuntosControl,    0*e,  -1*e, 0);

	 return PuntosControl;
}

void TramoTuberia::crearComponentes(GLuint program) {
	//Creo los tramos para generar un tramo de tuberia.
	//Estos son los caminos
	vector<float>* camino1 = new vector<float>();
	vector<float>* camino2 = new vector<float>();
		addPoint(camino1, 0.0,0.0,1.0);
		addPoint(camino1, 0.0,0.0,3.0);
		addPoint(camino1, 0.0,0.0,5.0);
		addPoint(camino1, 0.0,0.0,7.0);
		//Camino2
		addPoint(camino2, 0.0,0.0,7.0);
		addPoint(camino2, 0.0,0.0,7.0);
		addPoint(camino2, 0.0,0.0,7.0);
		addPoint(camino2, 0.0,1.0,9.0);
		addPoint(camino2, 0.0,2.0,9.0);
		addPoint(camino2, 0.0,3.0,7.0);
		addPoint(camino2, 0.0,3.0,7.0);
		addPoint(camino2, 0.0,3.0,7.0);
	this->tramos = new list<SweepSurf*>();
	vector<float>* puntosBarrido = createPoints();
	SweepSurf tramo1(program, puntosBarrido, camino1);
	SweepSurf tramo2(program, puntosBarrido, camino2);
	tramos->push_front(&tramo1);
	tramos->push_front(&tramo2);
	delete (puntosBarrido);
	delete (camino1);
	delete (camino2);
}

TramoTuberia::~TramoTuberia() {
	for(list<SweepSurf*>::iterator it = tramos->begin(); it != tramos->end(); it++) {
			delete((*it));
		}
	delete (tramos);
}

void TramoTuberia::draw(GLuint program, glm::mat4 & world_matrix) {
	glm::mat4 matriz = world_matrix * this->model_matrix;
	for(list<SweepSurf*>::iterator it = tramos->begin(); it != tramos->end(); it++) {
		//Dubujo todos los tramos.
		(*it)->draw(program,matriz);
	}
}

void TramoTuberia::set_texture_id(GLuint texture) {
	for(list<SweepSurf*>::iterator it = tramos->begin(); it != tramos->end(); it++) {
		//Asigno
		(*it)->set_texture_id(texture);
	}
}

